/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fujitsu.nanjing.tieba.base.BaseService;
import com.fujitsu.nanjing.tieba.ibator.CoreBoard;
import com.fujitsu.nanjing.tieba.ibator.CoreBoardCriteria;
import com.fujitsu.nanjing.tieba.ibator.CoreBoardWithBLOBs;
import com.fujitsu.nanjing.tieba.model.Board;

/**
 * BoardService.java
 * 
 * @author Qixiaobo-win8
 */

@Service
@Transactional
public class BoardService
		extends
			BaseService<CoreBoardWithBLOBs, CoreBoardCriteria> {
	@Resource
	private Board board;
	@Resource
	private TopicService topicService;
	@Resource
	private ReplyService replyService;
	@Override
	public CoreBoardWithBLOBs findById(String id) {
		return board.selectByPrimaryKey(Integer.parseInt(id));

	}

	@Override
	public CoreBoardWithBLOBs insert(CoreBoardWithBLOBs t) {
		board.insert(t);
		return t;
	}

	@Override
	public void update(CoreBoardWithBLOBs t) {
		board.updateByPrimaryKey(t);

	}

	@Override
	public void delete(CoreBoardWithBLOBs t) {
		board.deleteByPrimaryKey(t.getBoardid());

	}

	public List<CoreBoardWithBLOBs> findAllBySectionId(String sectionId) {
		CoreBoardCriteria example = new CoreBoardCriteria();
		example.createCriteria().andSectionidEqualTo(
				Integer.parseInt(sectionId));
		return board.selectByExampleWithBLOBs(example);
	}
	public int countTopicByBoardId(int boardId) {

		int count = topicService.countByBoardId(boardId);
		return count;
	}

	public int countRepliesByBoardId(int boardId) {
		return replyService.countRepliesByBoardId(boardId);
	}
	public int countBoards() {
		CoreBoardCriteria example = new CoreBoardCriteria();
		example.createCriteria().andSectionidIsNotNull();
		return board.countByExample(example);
	}

	public String findAll() {
		CoreBoardCriteria example = new CoreBoardCriteria();
		example.createCriteria().andSectionidIsNotNull();
		List<CoreBoard> boards = board.selectByExampleWithoutBLOBs(example);
		StringBuilder sb = new StringBuilder();
		for (CoreBoard coreBoard : boards) {
			sb.append(coreBoard.getBoardid());
			sb.append(",");
		}
		return sb.substring(0, sb.length() - 1);
	}
}
